package com.party.controller.user.myparty;

import com.party.common.Page;
import com.party.common.Tools;
import com.party.model.SysUser;
import com.party.model.UserInfo;
import com.party.service.info.InfoService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: beyond
 * Date: 14-10-8
 * Time: 下午10:27
 * To change this template use File | Settings | File Templates.
 */

@Controller
@RequestMapping(value = "/info")

public class InfoController {

    @Resource
    InfoService infoService;
    private int pageSize = 10;
    /**
     * 首页
     * @return
     */
    @RequestMapping(value = "index")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("user/info/info");
        return modelAndView;
    }

    @RequestMapping(value = "/query")
    @ResponseBody
    public Map<String,Object> query(HttpServletRequest req,HttpSession session) {
        Map<String, Object> map = new HashMap<String, Object>();
        String pageIndexStr = req.getParameter("pageIndex");
        String title = req.getParameter("title");
        String time = req.getParameter("time");
        UserInfo userInfo = new UserInfo();
        if ((SysUser) session.getAttribute("sysUser") != null) {
            Integer uid = ((SysUser) session.getAttribute("sysUser")).getUid();
            userInfo.setUid(uid);
        }
        int pageIndex = 1;

        if (pageIndexStr != null && !"".equals(pageIndexStr)) {
            pageIndex = Integer.parseInt(pageIndexStr);
        }
        userInfo.setTitle(title);
        userInfo.setTime(time);

        Page p = this.infoService.getUserInfoList(userInfo, pageSize, pageIndex);
        map.put("pageCount",p.getTotalPageCount() );
        map.put("tableData",p.getResult());
        return map;
    }

    /**
     * 增加
     * @return
     */
    @RequestMapping(value = "/add")
    public ModelAndView add() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/user/info/info_add");
        return modelAndView;
    }

    /**
     * 增加数据
     * @return
     */
    @RequestMapping(value = "/addData")
    @ResponseBody
    public Map<String,Object> addData(
            @RequestParam(value = "title")String title,
            @RequestParam(value = "type")String type,
            @RequestParam(value = "description")String description,
            @RequestParam(value = "content")String content,
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response) {
        UserInfo userInfo=new UserInfo();
        userInfo.setContent(content.getBytes());
        String msg="";
        if ((SysUser) session.getAttribute("sysUser") != null) {
            String realname = ((SysUser) session.getAttribute("sysUser")).getRealname();
            Integer uid = ((SysUser) session.getAttribute("sysUser")).getUid();
            userInfo.setAuthor(realname);
            userInfo.setUid(uid);
        }
        userInfo.setTitle(title);
        userInfo.setType(type);
        userInfo.setDescription(description);
        String time=Tools.getNowDate();
        userInfo.setTime(time);
        int num=infoService.addUserInfo(userInfo);
        if(num>0){
            msg="添加成功";
        }else{
            msg="添加失败";
        }
        Map<String, Object> map = new HashMap<String, Object>();

        map.put("msg",msg);
        return map;
    }

    /**
     * 编辑
     * @return
     */
    @RequestMapping(value = "/edit")
    public ModelAndView edit(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();
        String id=request.getParameter("id");
        String msg="";
        UserInfo userInfo=null;
        if(!"".equals(id)){
            int intId=Integer.parseInt(id);
            userInfo=infoService.getUserInfoByPrimaryKey(intId);
        }

        if(userInfo==null){
            msg="获取成功";
        }else{
            msg="获取失败";
        }
        String content=new String(userInfo.getContent());
        modelAndView.addObject("userInfo",userInfo);
        modelAndView.addObject("content",content);
        modelAndView.setViewName("/user/info/info_edit");
        return modelAndView;
    }

    /**
     * 编辑
     * @return
     */
    @RequestMapping(value = "/editData")
    @ResponseBody
    public Map<String,Object> editData(
            @RequestParam(value = "id")Integer id,
            @RequestParam(value = "title")String title,
            @RequestParam(value = "type")String type,
            @RequestParam(value = "description")String description,
            @RequestParam(value = "content")String content,
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response) {
        UserInfo userInfo=new UserInfo();
        userInfo.setId(id);
        userInfo.setTitle(title);
        userInfo.setDescription(description);
        userInfo.setType(type);
        userInfo.setContent(content.getBytes());
        String msg="";
        int num=infoService.updateUserInfoByPrimaryKey(userInfo);
        if(num>0){
            msg="编辑成功";
        }else{
            msg="编辑失败";
        }

        Map<String, Object> map = new HashMap<String, Object>();

        map.put("msg",msg);

        return map;
    }


    /**
     * 删除
     *
     * @param ids
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Map<String, Object> delete(
            @RequestParam("ids") String ids,
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response
    ) {

        int num = 0;
        try {
            num = infoService.deleteExamByIds(ids);

        } catch (Exception e) {
            num = -1;
            e.printStackTrace();
        }
        Map<String, Object> modelMap = new HashMap<String, Object>();
        modelMap.put("num", num);
        return modelMap;
    }

    @RequestMapping(value = "content")
    public ModelAndView content(
            HttpSession session,
            HttpServletRequest request
    ) {
        ModelAndView modelAndView = new ModelAndView();
        UserInfo userInfo = new UserInfo();
        String idStr = request.getParameter("id");
        if(idStr != null && !"".equals(idStr) && Tools.isNumber(idStr))
        {
            Integer id = Integer.parseInt(idStr);
            userInfo = this.infoService.getUserInfoByPrimaryKey(id);
        }
        modelAndView.addObject("obj",userInfo);
        byte[]binaryContent=userInfo.getContent();
        String content="";
        if(binaryContent!=null){
            content=new String(binaryContent);
        }

        modelAndView.addObject("content",content);
        modelAndView.setViewName("/user/info/info_content");
        return modelAndView;
    }
}

